package cn.minhx.domain.activity.service.trial.node;

import cn.minhx.domain.activity.model.entity.MarketProductEntity;
import cn.minhx.domain.activity.model.entity.TrialBalanceEntity;
import cn.minhx.domain.activity.model.valobj.GroupBuyActivityDiscountVO;
import cn.minhx.domain.activity.model.valobj.SkuVO;
import cn.minhx.domain.activity.service.trial.AbstractGroupBuyMarketSupport;
import cn.minhx.domain.activity.service.trial.factory.DefaultActivityStrategyFactory;
import cn.minhx.types.design.tree.StrategyHandler;
import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 结束节点
 *
 * @author minhx
 */
@Slf4j
@Service
public class EndNode extends AbstractGroupBuyMarketSupport {
	@Override
	protected TrialBalanceEntity doApply(MarketProductEntity requestParameter,
			DefaultActivityStrategyFactory.DynamicContext dynamicContext) throws Exception {
		log.info("拼团商品查询试算服务-EndNode requestParameter:{}", JSON.toJSONString(requestParameter));
		GroupBuyActivityDiscountVO groupBuyActivityDiscountVO = dynamicContext.getGroupBuyActivityDiscountVO();
		SkuVO skuVO = dynamicContext.getSkuVO();

		// 返回结果
		return TrialBalanceEntity.builder().goodsId(skuVO.getGoodsId()).goodsName(skuVO.getGoodsName())
				.originalPrice(skuVO.getOriginalPrice()).discountedPrice(dynamicContext.getDiscountedPrice())
				.targetCount(groupBuyActivityDiscountVO.getTarget())
				.startTime(groupBuyActivityDiscountVO.getStartTime()).endTime(groupBuyActivityDiscountVO.getEndTime())
				.isVisible(dynamicContext.isVisible()).isEnable(dynamicContext.isEnable())
				.groupBuyActivityDiscountVO(groupBuyActivityDiscountVO).build();
	}

	@Override
	public StrategyHandler<MarketProductEntity, DefaultActivityStrategyFactory.DynamicContext, TrialBalanceEntity> next(
			MarketProductEntity requestParameter, DefaultActivityStrategyFactory.DynamicContext dynamicContext)
			throws Exception {
		return defaultStrategyHandler;
	}
}
